﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SiteNavigation.Models
{
    public class ResponseViewModel
    {
        public bool IsSuccess { get; set; }
        public string Message { get; set; }
        public object Result { get; set; }

        public static ResponseViewModel Success(string message = "成功")
        {
            return new ResponseViewModel
            {
                IsSuccess = true,
                Message = message,
                Result = null
            };
        }

        public static ResponseViewModel Success(object result, string message = "成功")
        {
            return new ResponseViewModel
            {
                IsSuccess = true,
                Message = message,
                Result = result
            };
        }

        public static ResponseViewModel Fail(string message = "失败")
        {
            return new ResponseViewModel
            {
                IsSuccess = false,
                Message = message,
                Result = null
            };
        }

        public static ResponseViewModel Fail(Exception exception, string message = "失败")
        {
            if(exception is OperationFailedException)
            {
                return new ResponseViewModel
                {
                    IsSuccess = false,
                    Message = message,
                    Result = exception.Message
                };
            }
            else
            {
                return new ResponseViewModel
                {
                    IsSuccess = false,
                    Message = message,
                    Result = null
                };
            }
        }
    }
}
